package com.huawei.hwid.api.common;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient.FileChooserParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;
import com.huawei.android.app.ActionBarEx;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.l;
import com.huawei.hwid.core.d.o;
import com.huawei.hwid.d;
import com.huawei.hwid.usecase.CompressPictureCase;
import com.huawei.hwid.usecase.CompressPictureCase.RequestValues;
import com.huawei.hwid.vermanager.c;
import com.huawei.secure.android.common.intent.SafeBundle;
import com.huawei.secure.android.common.intent.SafeIntent;
import com.huawei.secure.android.common.webview.SafeWebView;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

public class CloudAccountCenterActivity extends Activity {
    private String a;
    private SafeWebView b;
    private ProgressBar c;
    private ActionBar d;
    private String e = "; phoneservice; cversion=hwid_advanced 3.0.3.301; Version=27120";
    private String f = "";
    private String g = "";
    private String h = "";
    private String i;
    private String j;
    private int k;
    private int l;
    private int m;
    private ValueCallback<?> n;
    private Uri o;
    private int p;
    private String q;
    private AlertDialog r;
    private boolean s = true;
    private com.huawei.cloudservice.a t = new com.huawei.cloudservice.a(this) {
        final /* synthetic */ CloudAccountCenterActivity a;

        {
            this.a = r1;
        }

        public void a(WebView webView, String str, Bitmap bitmap) {
            if (this.a.c != null) {
                this.a.c.setProgress(0);
                this.a.c.setVisibility(0);
            }
        }

        public void a(WebView webView, String str) {
            if (this.a.c != null) {
                this.a.c.setProgress(this.a.c.getMax());
                this.a.c.setVisibility(8);
            }
            this.a.f();
        }
    };

    final class JavaScriptLocalObj {
        final /* synthetic */ CloudAccountCenterActivity a;

        JavaScriptLocalObj(CloudAccountCenterActivity cloudAccountCenterActivity) {
            this.a = cloudAccountCenterActivity;
        }

        @JavascriptInterface
        public void intoApp(String str) {
            e.b("CloudAccountCenterActivity", "enter intoApp", true);
            new Handler(this.a.getMainLooper()).post(new Runnable(this) {
                final /* synthetic */ JavaScriptLocalObj a;

                {
                    this.a = r1;
                }

                public void run() {
                    e.b("CloudAccountCenterActivity", "intoApp finish CloudAccountCenterActivity", true);
                    this.a.a.finish();
                }
            });
        }
    }

    class MyWebChromeClient extends WebChromeClient {
        final /* synthetic */ CloudAccountCenterActivity a;

        private MyWebChromeClient(CloudAccountCenterActivity cloudAccountCenterActivity) {
            this.a = cloudAccountCenterActivity;
        }

        public void onProgressChanged(WebView webView, int i) {
            super.onProgressChanged(webView, i);
            if (this.a.c != null) {
                this.a.c.setProgress(i);
                if (i == 100) {
                    this.a.c.setVisibility(8);
                } else {
                    this.a.c.setVisibility(0);
                }
            }
        }

        public void onReceivedTitle(WebView webView, String str) {
            super.onReceivedTitle(webView, str);
            this.a.a(str);
        }

        public void openFileChooser(ValueCallback<Uri> valueCallback) {
            e.b("CloudAccountCenterActivity", "SuppressWarnings openFileChooser For Android < 3.0", true);
            this.a.q = "";
            this.a.a((ValueCallback) valueCallback, this.a.q);
        }

        public void openFileChooser(ValueCallback<Uri> valueCallback, String str) {
            e.b("CloudAccountCenterActivity", "openFileChooser For Android 3.0+", true);
            this.a.q = str;
            this.a.a((ValueCallback) valueCallback, this.a.q);
        }

        public void openFileChooser(ValueCallback<Uri> valueCallback, String str, String str2) {
            e.b("CloudAccountCenterActivity", "openFileChooser For Android > 4.1", true);
            this.a.q = str;
            if (this.a.c()) {
                o.a(this.a.getApplicationContext(), l.a(this.a, "hwid_string_not_support_split"));
                if (valueCallback != null) {
                    try {
                        valueCallback.onReceiveValue(null);
                        return;
                    } catch (Exception e) {
                        e.b("CloudAccountCenterActivity", "isInMultiWindowMode openFileChooser e", true);
                        return;
                    }
                }
                return;
            }
            this.a.a((ValueCallback) valueCallback, this.a.q);
        }

        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, FileChooserParams fileChooserParams) {
            e.b("CloudAccountCenterActivity", "onShowFileChooser For Android > 5.0", true);
            this.a.n = valueCallback;
            if (this.a.c()) {
                o.a(this.a.getApplicationContext(), l.a(this.a, "hwid_string_not_support_split"));
                if (valueCallback != null) {
                    try {
                        valueCallback.onReceiveValue(new Uri[0]);
                    } catch (Exception e) {
                        e.b("CloudAccountCenterActivity", "isInMultiWindowMode openFileChooser e", true);
                    }
                }
            } else {
                this.a.q = this.a.a(fileChooserParams);
                this.a.p = 1002;
                if (this.a.b(1002)) {
                    this.a.a(1002);
                }
            }
            return true;
        }
    }

    class a extends Thread {
        final /* synthetic */ CloudAccountCenterActivity a;

        private a(CloudAccountCenterActivity cloudAccountCenterActivity) {
            this.a = cloudAccountCenterActivity;
        }

        public void run() {
            com.huawei.hwid.f.e.e(this.a.getApplicationContext());
        }
    }

    private static boolean a(int[] iArr) {
        if (iArr == null || iArr.length <= 0) {
            return false;
        }
        for (int i : iArr) {
            if (i != 0) {
                return false;
            }
        }
        return true;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        o.a(this);
        setContentView(l.c(this, "cs_webview"));
        b();
        a();
        o.b(this);
        a("");
    }

    @SuppressLint({"SetJavaScriptEnabled"})
    private void a() {
        this.d = getActionBar();
        if (this.d != null) {
            if (o.a && b.h("com.huawei.android.app.ActionBarEx")) {
                try {
                    ActionBarEx.setStartIcon(this.d, true, null, new OnClickListener(this) {
                        final /* synthetic */ CloudAccountCenterActivity a;

                        {
                            this.a = r1;
                        }

                        public void onClick(View view) {
                            this.a.finish();
                        }
                    });
                    this.d.setDisplayHomeAsUpEnabled(false);
                } catch (Exception e) {
                    this.d.setDisplayHomeAsUpEnabled(true);
                }
            } else {
                this.d.setDisplayHomeAsUpEnabled(true);
            }
        }
        this.c = (ProgressBar) findViewById(l.d(this, "wvProgressbar"));
        this.b = (SafeWebView) findViewById(l.d(this, "webView"));
        if (this.b != null) {
            ArrayList b = com.huawei.hwid.d.b.a().b(this);
            if (b == null) {
                b = new ArrayList(0);
            }
            String[] strArr = (String[]) b.toArray(new String[b.size()]);
            e.b("CloudAccountCenterActivity", "White list: " + Arrays.toString(strArr), false);
            this.b.setWhitelist(strArr);
            this.b.setWebViewClient(c.a(this, this.t));
            this.b.setWebChromeClient(new MyWebChromeClient());
            this.b.addJavascriptInterface(new JavaScriptLocalObj(this), "webLoader");
            WebSettings settings = this.b.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setAllowFileAccess(false);
            settings.setSupportZoom(false);
            settings.setSavePassword(false);
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            if (VERSION.SDK_INT >= 21) {
                settings.setMixedContentMode(1);
            }
            settings.setGeolocationEnabled(false);
            settings.setAllowContentAccess(false);
            settings.setUserAgentString(settings.getUserAgentString() + this.e);
            this.b.removeJavascriptInterface("searchBoxJavaBridge_");
            this.b.removeJavascriptInterface("accessibility");
            this.b.removeJavascriptInterface("accessibilityTraversal");
            e.b("CloudAccountCenterActivity", "webView url: " + this.a, false);
            this.b.loadUrl(this.a);
        }
    }

    private void b() {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }
        Intent intent2;
        if (intent instanceof SafeIntent) {
            intent2 = intent;
        } else {
            intent2 = new SafeIntent(intent);
            setIntent(intent2);
        }
        Bundle extras = intent2.getExtras();
        if (extras == null) {
            finish();
            return;
        }
        SafeBundle safeBundle = new SafeBundle(extras);
        try {
            this.s = !HwAccountConstants.ACTION_ACCOUNT_FIND_PWD_CLOUD.equalsIgnoreCase(extras.getString(HwAccountConstants.KEY_ENTER_CENTER));
            this.f = safeBundle.getString(CloudAccountManager.KEY_APP_ID_WEB);
            this.g = safeBundle.getString(CloudAccountManager.KEY_USER_ACCOUNT_WEB);
            this.h = safeBundle.getString(CloudAccountManager.KEY_SERVICE_TOKEN_WEB);
            this.i = safeBundle.getString("deviceId");
            this.j = safeBundle.getString("deviceType");
            this.m = safeBundle.getInt("siteId");
            this.k = safeBundle.getInt(CloudAccountManager.KEY_REQCLIENTTYPE);
            this.l = safeBundle.getInt(CloudAccountManager.KEY_LOGIN_CHANNEL);
        } catch (RuntimeException e) {
            e.d("CloudAccountCenterActivity", "RuntimeException", true);
        } catch (Exception e2) {
            e.d("CloudAccountCenterActivity", "Exception", true);
        }
        this.a = this.s ? a((Context) this) : b((Context) this);
        if (TextUtils.isEmpty(this.a)) {
            finish();
            return;
        }
        try {
            e.d("CloudAccountCenterActivity", "temUrl host:" + new URL(this.a).getHost(), true);
        } catch (MalformedURLException e3) {
            e.d("CloudAccountCenterActivity", "MalformedURLException", true);
        }
        if (!this.a.toLowerCase(Locale.US).startsWith("https://")) {
            this.a = "https://" + this.a;
        }
    }

    private void a(String str) {
        if (this.d != null) {
            this.d.setTitle(str);
        }
    }

    private void a(int i) {
        this.o = com.huawei.hwid.f.e.a((Context) this);
        if (this.o != null) {
            e.b("CloudAccountCenterActivity", "dialog cancel mTmpPicUri ", true);
            this.p = i;
            this.r = new c(this, this.o).show();
            this.r.setOnCancelListener(new OnCancelListener(this) {
                final /* synthetic */ CloudAccountCenterActivity a;

                {
                    this.a = r1;
                }

                public void onCancel(DialogInterface dialogInterface) {
                    e.b("CloudAccountCenterActivity", "dialog cancel", true);
                    this.a.a(null);
                }
            });
        }
    }

    private boolean b(int i) {
        if (a((Activity) this, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.CAMERA"}, i)) {
            return true;
        }
        return false;
    }

    private void a(Uri uri) {
        e.b("CloudAccountCenterActivity", "RESULTCODE", true);
        if (this.n != null) {
            try {
                if (this.p == 1001) {
                    this.n.onReceiveValue(uri);
                } else if (this.p == 1002) {
                    this.n.onReceiveValue(uri == null ? new Uri[0] : new Uri[]{uri});
                }
            } catch (RuntimeException e) {
                e.b("CloudAccountCenterActivity", "RuntimeException e", true);
            } catch (Exception e2) {
                e.d("CloudAccountCenterActivity", "Exception", true);
            }
            this.n = null;
        }
    }

    private void a(ValueCallback<Uri> valueCallback, String str) {
        this.n = valueCallback;
        this.p = 1001;
        if (b(1001)) {
            a(1001);
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        boolean z = true;
        e.b("CloudAccountCenterActivity", "onActivityResult", true);
        super.onActivityResult(i, i2, intent);
        if (i == 1004 || i == 1003) {
            if (!(i != 1004 || intent == null || intent.getData() == null)) {
                this.o = intent.getData();
            }
            if (-1 != i2) {
                z = false;
            }
            if (a(z)) {
                b(this.o);
            }
        }
    }

    private void b(Uri uri) {
        e.b("CloudAccountCenterActivity", "startCompressPic", true);
        new com.huawei.hwid.b(d.a()).a(new CompressPictureCase(this), new RequestValues(uri), new com.huawei.hwid.UseCase.a(this) {
            final /* synthetic */ CloudAccountCenterActivity a;

            {
                this.a = r1;
            }

            public void a(Bundle bundle) {
                this.a.a((Uri) bundle.getParcelable("request_pic_uri_tag"));
            }

            public void b(Bundle bundle) {
                this.a.a(null);
            }
        });
    }

    private boolean a(boolean z) {
        if (this.n == null) {
            return false;
        }
        if (z && this.o != null) {
            return true;
        }
        e.b("CloudAccountCenterActivity", "onReceiveValue = null", true);
        a(null);
        return false;
    }

    @TargetApi(23)
    private boolean a(Activity activity, String[] strArr, int i) {
        if (VERSION.SDK_INT <= 22 || activity == null || strArr == null || strArr.length == 0) {
            return true;
        }
        ArrayList arrayList = new ArrayList();
        for (String str : strArr) {
            if (activity.checkSelfPermission(str) != 0) {
                arrayList.add(str);
            }
        }
        if (arrayList.size() <= 0) {
            return true;
        }
        activity.requestPermissions((String[]) arrayList.toArray(new String[arrayList.size()]), i);
        return false;
    }

    @TargetApi(21)
    private String a(FileChooserParams fileChooserParams) {
        if (fileChooserParams == null) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (String append : fileChooserParams.getAcceptTypes()) {
            stringBuffer.append(append);
        }
        return stringBuffer.toString();
    }

    private boolean c() {
        try {
            return ((Boolean) Class.forName("android.app.Activity").getMethod("isInMultiWindowMode", new Class[0]).invoke(this, new Object[0])).booleanValue();
        } catch (ClassNotFoundException e) {
            e.b("CloudAccountCenterActivity", "ClassNotFoundException", true);
            return false;
        } catch (NoSuchMethodException e2) {
            e.b("CloudAccountCenterActivity", "NoSuchMethodException", true);
            return false;
        } catch (IllegalAccessException e3) {
            e.b("CloudAccountCenterActivity", "IllegalAccessException", true);
            return false;
        } catch (IllegalArgumentException e4) {
            e.b("CloudAccountCenterActivity", "IllegalArgumentException", true);
            return false;
        } catch (InvocationTargetException e5) {
            e.b("CloudAccountCenterActivity", "InvocationTargetException", true);
            return false;
        } catch (RuntimeException e6) {
            e.b("CloudAccountCenterActivity", "RuntimeException", true);
            return false;
        } catch (Exception e7) {
            e.b("CloudAccountCenterActivity", "Exception", true);
            return false;
        }
    }

    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        if (i != 1001 && i != 1002) {
            return;
        }
        if (a(iArr)) {
            a(i);
            return;
        }
        d();
        a(null);
    }

    private void d() {
        if (VERSION.SDK_INT > 22) {
            String string;
            int i = checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") != 0 ? 1 : 0;
            int i2;
            if (checkSelfPermission("android.permission.CAMERA") != 0) {
                i2 = 1;
            } else {
                i2 = 0;
            }
            if (i != 0 && r3 != 0) {
                string = getResources().getString(l.a(this, "hwid_string_permission_and"), new Object[]{getResources().getString(l.a(this, "hwid_string_permission_storage")), getResources().getString(l.a(this, "hwid_string_permission_camera"))});
            } else if (i != 0) {
                string = getResources().getString(l.a(this, "hwid_string_permission_show"), new Object[]{getResources().getString(l.a(this, "hwid_string_permission_storage"))});
            } else {
                string = getResources().getString(l.a(this, "hwid_string_permission_show"), new Object[]{getResources().getString(l.a(this, "hwid_string_permission_camera"))});
            }
            Builder a = o.a(this, string, getResources().getString(l.a(this, "hwid_string_permission_use_appeal")));
            if (!isFinishing()) {
                Dialog create = a.create();
                o.a(create);
                create.show();
            }
        }
    }

    protected void onPause() {
        super.onPause();
        if (this.b != null) {
            this.b.onPause();
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        if (this.b != null) {
            this.b.setVisibility(8);
            this.b.removeAllViews();
            this.b.destroy();
        }
        this.b = null;
        if (this.r != null) {
            this.r.dismiss();
            this.r = null;
        }
        new a().start();
    }

    public void onBackPressed() {
        if (!e()) {
            super.onBackPressed();
        }
    }

    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
    }

    private boolean e() {
        if (this.b == null || !this.b.canGoBack()) {
            return false;
        }
        String url = this.b.getUrl();
        if (url == null || !(url.endsWith("index.html") || url.endsWith("wapLogin.html"))) {
            this.b.goBack();
            return true;
        }
        e.b("CloudAccountCenterActivity", "can not go back", true);
        return false;
    }

    protected void onResume() {
        super.onResume();
        if (this.b != null) {
            this.b.onResume();
        }
    }

    private void f() {
        if (this.s) {
            e.b("CloudAccountCenterActivity", "autoLogin", true);
            this.b.loadUrl("javascript:autoLogin(\"" + this.f + "\",\"" + this.g + "\",\"" + this.h + "\")");
        }
    }

    private String a(Context context) {
        String b = c.a().b(context, this.m);
        String c = c.a().c(context, this.m);
        StringBuffer stringBuffer = new StringBuffer(c.a().a(context, this.m) + "?Version=" + "27120" + "&cversion=hwid_advanced " + HwAccountConstants.SDK_VERSION);
        try {
            b = URLEncoder.encode(b, "UTF-8");
            c = URLEncoder.encode(c, "UTF-8");
            stringBuffer.append("&loginUrl=").append(b).append("&deviceID=").append(this.i).append("&deviceType=").append(this.j).append("&reqClientType=").append(this.k).append("&service=").append(c).append("&loginChannel=").append(this.l).append("&lang=").append(URLEncoder.encode(b.f(context).toLowerCase(Locale.getDefault()), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            stringBuffer = new StringBuffer();
        } catch (Exception e2) {
            stringBuffer = new StringBuffer();
        }
        return stringBuffer.toString();
    }

    private String b(Context context) {
        StringBuffer stringBuffer = new StringBuffer(c.a().a(context) + "?Version=" + "27120" + "&cversion=hwid_advanced " + HwAccountConstants.SDK_VERSION);
        try {
            stringBuffer.append("&reqClientType=").append(this.k).append("&loginChannel=").append(this.l).append("&lang=").append(URLEncoder.encode(b.f(context).toLowerCase(Locale.getDefault()), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            stringBuffer = new StringBuffer();
        } catch (Exception e2) {
            stringBuffer = new StringBuffer();
        }
        return stringBuffer.toString();
    }
}
